<?php
//    error_reporting(E_ALL & ~E_DEPRECATED);
    error_reporting(0);
    require_once("condb.php");
    require_once("functions_ring.php");  
    
    $location = getcwd();
    $location = substr($location, 0, strpos($location, "data_app")) . "upload/";    
    
    /*
    * Lấy cú pháp sms
    */
    
    $syntax = getSmsSyntax();
    
    /* sms */
    $sms[0]["id"] = "1";
    $sms[0]["message"] = "Nhắn tin để tải nhạc chờ";
    $sms[0]["syntax"] = $syntax[6]["syntax"];
    $sms[0]["number"] = $syntax[6]["serviceCode"];
    
    $sms[1]["id"] = "2";
    $sms[1]["message"] = "Nhắn tin để cài nhạc chờ";
    $sms[1]["syntax"] = $syntax[4]["syntax"];
    $sms[1]["number"] = $syntax[4]["serviceCode"];
    
    $sms[2]["id"] = "3";
    $sms[2]["message"] = "Nhắn tin để tặng nhạc chờ";
    $sms[2]["syntax"] = $syntax[5]["syntax"];
    $sms[2]["number"] = $syntax[5]["serviceCode"]; 
    
    $output = array();
    
    $info = isset($_GET["info"]) ? $_GET["info"] : "";
    if(empty($info)){
        $output["status"] = 2;
        $output["msg"] = "Invalid Request";
    } else {
        $info = urldecode(base64_decode($info));
        parse_str($info, $request);    
        
        $token = isset($request["token"]) ? cleanQuery($request["token"]) : "";
        if($token=="RingTAovieC@)!#"){
            $type = isset($request["type"]) ? cleanQuery($request["type"]) : "";
            if($type=="get_categories"){
                
                /*
                * Lấy danh mục nhạc chờ
                */
                
                $categories = getCategories();
                $category = array();
                foreach($categories as $i=>$row){
                    $category[$i]["id"] = $row["id"];   
                    $category[$i]["name"] = $row["title"];   
                    $category[$i]["icon_url"] = getImage($row["picture"], "ringBack", $row["create_date"], "");   
                }
                
                $output["status"] = 1;
                $output["msg"] = "Success Request";
                
            } else if($type=="get_song") {
                
                /*
                * Lấy danh sách bài hát
                */ 
                
                $categoryId = isset($request["id"]) ? intval($request["id"]) : 0;                   
                $currentPage = isset($request["p"]) ? intval($request["p"]) : 1;   
                $currentPage = $currentPage==0 ? 1 : $currentPage;
                $numberRecordPerPage = isset($request["record"]) ? intval($request["record"]) : 0;   
                                
                if($categoryId==0 || $numberRecordPerPage==0){
                    $output["status"] = 5;
                    $output["msg"] = "Invalid Request";   
                } else {
                    $startRecord = ($currentPage - 1) * $numberRecordPerPage;
                    
                    $ring = GetRing($categoryId, $startRecord, $numberRecordPerPage);
                    $totalRing = GetTotalRing($categoryId);
                    $numberPage = ceil($totalRing / $numberRecordPerPage);
                    
                    $data = array();
                    $data["type"] = "item";
                    $data["totalPage"] = $numberPage;
                    $data["currentPage"] = $currentPage;
                    $data["item"] = array();
                    foreach($ring as $i=>$row){
                        $data["item"][$i]["id"] = $row["id"];
                        $data["item"][$i]["song_name"] = $row["title"];
                        $data["item"][$i]["singer_name"] = $row["singer_name"];
                        $data["item"][$i]["download"] = $row["download"];
                    }
                    
                    $output["status"] = 1;
                    $output["msg"] = "Success Request";
                }
                                           
            } else if($type=="get_song_lastest") {
                
                /*
                * Lấy danh sách bài hát
                */ 
                                
                $currentPage = isset($request["p"]) ? intval($request["p"]) : 1;   
                $currentPage = $currentPage==0 ? 1 : $currentPage;
                $numberRecordPerPage = isset($request["record"]) ? intval($request["record"]) : 0;   
                                
                if($numberRecordPerPage==0){
                    $output["status"] = 5;
                    $output["msg"] = "Invalid Request";   
                } else {
                    $startRecord = ($currentPage - 1) * $numberRecordPerPage;
                    
                    $ring = GetRingLastest($startRecord, $numberRecordPerPage);
                    $totalRing = GetTotalRingLastest();
                    $numberPage = ceil($totalRing / $numberRecordPerPage);
                    
                    $data = array();
                    $data["type"] = "item";
                    $data["totalPage"] = $numberPage;
                    $data["currentPage"] = $currentPage;
                    $data["item"] = array();
                    foreach($ring as $i=>$row){
                        $data["item"][$i]["id"] = $row["id"];
                        $data["item"][$i]["song_name"] = $row["title"];
                        $data["item"][$i]["singer_name"] = $row["singer_name"];
                        $data["item"][$i]["download"] = $row["download"];
                    }
                    
                    $output["status"] = 1;
                    $output["msg"] = "Success Request";
                }
                
            } else if($type=="get_top_download") {
                
                /*
                * Lấy danh sách bài hát
                */ 
                                
                $currentPage = isset($request["p"]) ? intval($request["p"]) : 1;   
                $currentPage = $currentPage==0 ? 1 : $currentPage;
                $numberRecordPerPage = isset($request["record"]) ? intval($request["record"]) : 0;   
                                
                if($numberRecordPerPage==0){
                    $output["status"] = 5;
                    $output["msg"] = "Invalid Request";   
                } else {
                    $startRecord = ($currentPage - 1) * $numberRecordPerPage;
                    
                    $ring = GetRingTopDownload($startRecord, $numberRecordPerPage);
                    $totalRing = GetTotalRingTopDownload();
                    $numberPage = ceil($totalRing / $numberRecordPerPage);
                    
                    $data = array();
                    $data["type"] = "item";
                    $data["totalPage"] = $numberPage;
                    $data["currentPage"] = $currentPage;
                    $data["item"] = array();
                    foreach($ring as $i=>$row){
                        $data["item"][$i]["id"] = $row["id"];
                        $data["item"][$i]["song_name"] = $row["title"];
                        $data["item"][$i]["singer_name"] = $row["singer_name"];
                        $data["item"][$i]["download"] = $row["download"];
                    }
                    
                    $output["status"] = 1;
                    $output["msg"] = "Success Request";
                }
                
            } else if($type=="search") {
                
                /*
                * tìm kiếm
                */
                $currentPage = isset($request["p"]) ? intval($request["p"]) : 1;   
                $currentPage = $currentPage==0 ? 1 : $currentPage;
                $numberRecordPerPage = isset($request["record"]) ? intval($request["record"]) : 0; 
                $song_name = isset($request["song_name"]) ? cleanQuery($request["song_name"]) : ""; 
                $singer_name = isset($request["singer_name"]) ? cleanQuery($request["singer_name"]) : ""; 
                
                if($numberRecordPerPage==0){
                    $output["status"] = 5;
                    $output["msg"] = "Invalid Request";   
                } else {
                    $startRecord = ($currentPage - 1) * $numberRecordPerPage;
                    
                    $ring = GetRingSearch($song_name, $singer_name, $startRecord, $numberRecordPerPage);
                    $totalRing = GetTotalRingSearch($song_name, $singer_name);
                    $numberPage = ceil($totalRing / $numberRecordPerPage);
                    
                    $data = array();
                    $data["type"] = "item";
                    $data["totalPage"] = $numberPage;
                    $data["currentPage"] = $currentPage;
                    $data["item"] = array();
                    foreach($ring as $i=>$row){
                        $data["item"][$i]["id"] = $row["id"];
                        $data["item"][$i]["song_name"] = $row["title"];
                        $data["item"][$i]["singer_name"] = $row["singer_name"];
                        $data["item"][$i]["download"] = $row["download"];
                    }
                    
                    $output["status"] = 1;
                    $output["msg"] = "Success Request";
                }
                
            } else if($type=="play_music") {
                
                /*
                * Nhạc chờ chi tiết
                */                
                $songId = isset($request["id"]) ? intval($request["id"]) : 0;
                
                if($songId==0){
                    $output["status"] = 5;
                    $output["msg"] = "Invalid Request";   
                } else {
                    
                    $ring = GetRingById($songId);
                    $song = array();
                    if(!empty($ring)) {
                        $song["id"] = $songId;
                        $song["title"] = $ring["title"];
                        $song["url"] = getImage($ring["file"], "ringBack", $ring["create_date"], "");
                        $song["filezise"] = filesize($location . "ringBack" . date("/Y/md/", $ring["create_date"]) . $ring["file"]);
                        $song["lyrics"] = strip_tags($ring["lyrics"]);
                        $song["singer_name"] = $ring["singer_name"];                        
                    }
                    
                    /* Cập nhật lượt hit */
                    UpdateHit($songId);
                    
                    $output["status"] = 1;
                    $output["msg"] = "Success Request";
                }
                 
            } else{
                $output["status"] = 3;
                $output["msg"] = "Invalid Request";    
            } 
        } else {
            $output["status"] = 4;
            $output["msg"] = "Invalid Request";    
        }
    }
    
    $output["type"] = isset($request["type"]) ? cleanQuery($request["type"]) : "";
    $output["sms"] = isset($sms) ? $sms : array();
    $output["categories"] = isset($category) ? $category : array();
    $output["data"] = isset($data) ? $data : array();    
    $output["song"] = isset($song) ? $song : array();    
      
    echo json_encode($output);
?>
